﻿Namespace EIBData.DPT
    Public MustInherit Class EIBDataDPT_26_Base
        Inherits EIBDataDPT_Base

        Protected _active As Boolean = Nothing
        Protected _scene As Byte = Nothing


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data
            _active = (data(1) And &H4) >> 6
            _scene = data(1) And &H3F
        End Sub

        Sub New(active As Boolean, scene As Byte)
            If scene > &H3F Then Throw New EIB.Exceptions.EIBDataEncodeException("")

            _active = active
            _scene = scene

            MyBase._data = New Byte() {0,
                                       (If(_active, 1, 0) << 6) + (_scene And &H3F)
                                      }
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = DPT_OctetLength
        End Function

        Public Overrides Function ToString() As String
            Return String.Format("Active: {0}, SceneNumber: {1}", _active, _scene)
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_active, _scene}
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(Boolean), GetType(Byte)}
            End Get
        End Property

        Public Overrides Function ToInfoList() As List(Of EIB.InfoBase)
            Dim list As New List(Of EIB.InfoBase)

            list.Add(New EIB.Info("Type", DPT_Name))
            list.Add(New EIB.Info("Active", _active))
            list.Add(New EIB.Info("SceneNumber", _scene))

            Return list
        End Function

        Public Shared Shadows ReadOnly Property DPT_BaseName As String
            Get
                Return "r1B1U6"
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_OctetLength As Byte
            Get
                Return 2
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_Name As String
            Get
                Return "DPT_26"
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_Number As String
            Get
                Return "26.xxx"
            End Get
        End Property

        Public ReadOnly Property Active As Boolean
            Get
                Return _active
            End Get
        End Property

        Public ReadOnly Property SceneNumber As Byte
            Get
                Return _scene
            End Get
        End Property
    End Class
End Namespace